package com.fz.song.bigdatecreate.job;

import cn.hutool.core.util.RandomUtil;
import com.fz.song.bigdatecreate.entity.User;
import com.fz.song.bigdatecreate.service.IUserService;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.ArrayList;

@Slf4j
public class SaveUser implements Runnable {

    private IUserService userService;

    public SaveUser(IUserService userService) {
        this.userService = userService;
    }


    @Override
    public void run() {
        ArrayList<User> list = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            long start = System.currentTimeMillis();
            User user = new User();
            user.setUserId("mcid_" + RandomUtil.randomString(25));
            user.setUserName(RandomUtil.randomString(25));
            user.setAvatar(RandomUtil.randomNumbers(8) + ".jpg");
            user.setEmail(RandomUtil.randomString(25) + "@qq.com");
            user.setMobile("1" + (i % 8 + 1) + RandomUtil.randomNumbers(9));
            user.setStatus(1);
            user.setType((i % 2) == 0 ? 1 : 2);
            user.setAuditer(0);
            user.setAuditStatus(-1);
            user.setAuditTime(LocalDateTime.now());
            user.setCreateTime(LocalDateTime.now());
            user.setUpdateTime(LocalDateTime.now());
            list.add(user);
            if (i % 1000 == 0) {
                try {
                    userService.saveBatch(list);
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
                list = new ArrayList<>();
            }
            long end = System.currentTimeMillis();
            long l1 = end - start;
            log.info("{},耗时:{}", Thread.currentThread().getName(), l1);
        }
    }
}
